#ifndef TEST_ARRAY_H_
#define TEST_ARRAY_H_
#include "protomsg_config.h"
#include <stdio.h>


class testing_class {
public:
  testing_class () {
    printf("ctor\n");
  }
  ~testing_class() {
    printf("dtor\n");
  }

  testing_class( const testing_class& tt ) {
    a= 1990;
    printf("cctor\n");
  }
  size_t a;
};
void test() {
  {
    using protomsg::util::DynamicArray;
    typedef DynamicArray<testing_class> CDynamicArray;
    CDynamicArray darray0 , darray1(1) , darray2(1000);
    assert( darray0.capability() == 4 );
    assert( darray1.capability() == 4 );
    assert( darray2.capability() == 1000 );
    assert( darray1.empty() && darray1.size() ==0 );

#if 0 // push & pop
    testing_class tc;
    for( size_t i = 0 ; i < 10 ; ++i )
      darray0.Push( tc );
    for( size_t i = 0 ; i < 10 ; ++i ) {
      darray0.Pop();
    }

    for( size_t i = 0 ; i < 1000 ; ++i )
      darray0.Push(tc);
    darray0.Clear();
#endif


      // Index and Reserve
//     darray2.Index(2);
// 
//     darray2.Reserve(20000);
// 
//     for( size_t i = 0 ; i < darray2.size() ; ++i )
//       printf("%d.\n",darray2[i].a);
//     assert( darray2.capability() == 20000 );
//     ::getchar();
//     darray2.Clear();
//     darray2.Clear(true);
    darray2.Clear(true);
    darray2.Index(2);
    darray2.Index(1);

    ::getchar();

#if 0 // Clear and other 

    darray0.Clear();
    assert( darray0.capability() >1000 );
    assert(darray0.empty());
    darray0.Clear(true);
    assert(darray0.capability() ==4 );

    for( size_t i = 0 ; i < 1000 ; ++i )
      darray2.Push(i);
    for( size_t i = 0 ; i < 1000 ; ++i ) {
      printf("%d\n",darray2[i]);
    }

    darray2.Clear();
    assert( darray2.capability() == 1000 );
    assert( darray2.empty() && darray2.size() == 0 );

#endif

#if 0 // swap
    testing_class tt;

    // situation 2&&3
    for( size_t i = 0 ; i <1000 ; ++i )
      darray1.Push( tt );
    size_t cap = darray1.capability();
    darray0.Clear(true);
    darray0.Swap(darray1);
    assert( darray0.capability() ==cap && darray0.size() == 1000  );
    assert( darray1.capability() ==4 && darray1.size() == 0 );
    darray0.Swap(darray1);
    assert( darray1.capability() ==cap && darray1.size() == 1000  );
    assert( darray0.capability() ==4 && darray0.size() == 0 );

    // situation 1 
    darray1.Clear(true);
    darray1.Push(tt);
    darray0.Swap(darray1);
    assert( darray1.capability() == 4 && darray0.capability() == 4 );
    assert( darray0.size() == 1 && darray1.size() == 0 );
    // situation 4
    darray1.Index(100);
    darray2.Index(1000);
    darray1.Swap(darray2);
    assert( darray1.size() == 1000 && darray2.size() == 100 );
#endif
  }

  getchar();
}

#endif// TEST_ARRAY_H_